package com.zhongyi.http.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zhongyi.db.DBManager;
import com.zhongyi.entity.Company;
import com.zhongyi.entity.Contact;
import com.zhongyi.entity.SysLog;
import com.zhongyi.http.service.MailService;
import com.zhongyi.util.Constant;
import com.zhongyi.util.MailUtil;
import com.zhongyi.util.Pager;

public class MailServiceImpl implements MailService {
	
	private DBManager dbManager ;
	private MailUtil mailManager;
	
	public void setDbManager(DBManager dbManager) {
		this.dbManager = dbManager;
	}
	public void setMailManager(MailUtil mailManager) {
		this.mailManager = mailManager;
	}

	@Override
	public List<Contact> queryMailList(String pagerSQL, Pager pager) {
		// TODO Auto-generated method stub
		return dbManager.queryForBeanList(Constant.EMAIL_PAGER_SQL, new Object[]{pager.getStartNum(),pager.getPageSize()}, Contact.class);
	}
	
	@Override
	public List<Contact> queryMailList(String pagerSQL, Object[] params) {
		// TODO Auto-generated method stub
		return dbManager.queryForBeanList(Constant.EMAIL_PAGER_SQL, params, Contact.class);
	}

	@Override
	public List queryMailByStatus(String status) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List queryForList(String query) {
		// TODO Auto-generated method stub
		return dbManager.queryForMapList(query, null);
	}

	@Override
	public List<Contact> getMailByIds(String querySQL,String[] ids) {
		List<Contact> contactList = new ArrayList<Contact>();
		for(String mid : ids){
			contactList.addAll(dbManager.queryForBeanList(querySQL, new String[]{mid}, Contact.class));
		}
		return contactList;
	}
	
	
	@Override
	public List<Company> getCorpListById(String querySQL,String[] corpIds) {
		// TODO Auto-generated method stub
		List<Company> corpList = new ArrayList<Company>();
		for(String cid : corpIds){
			corpList.addAll(dbManager.queryForBeanList(querySQL, new String[]{cid}, Company.class));
		}
		return corpList;
	}
	
	@Override
	public boolean sendHtmlMailList(List<Contact> mailList, String subject,
			String message, String content, String mailType) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public boolean sendHtmlMail(Contact mail, String subject, String message,
			String content, String mailType) {
		// TODO Auto-generated method stub
		return mailManager.sendHtmlMail(mail.getContact_info(), subject, message, content);
	}
	@Override
	public void updateMailStatus(String updateSQL,Contact contact,String status) {
		// TODO Auto-generated method stub
		dbManager.updateDB(updateSQL, new String[]{status,contact.getId()});
	}
	@Override
	public void insertToSysLog(String insertSQL, SysLog sysLog) {
		// TODO Auto-generated method stub
		dbManager.insertToDB(Constant.SYSLOG_INSERT_SQL, new Object[]{sysLog.getId(),sysLog.getContact_id(),sysLog.getContact_info(),sysLog.getInfo_type(),sysLog.getInfo_body(),sysLog.getContact_time(),sysLog.getRemark(),sysLog.getStatus()});
		
	}
	
}
